﻿using CodeGenerator.Models;
using CodeGenerator.Util;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CodeGenerator.Template.Web.Controllers
{
    public partial class Controller : ITemplate
    {
        public MetaData Data { get; set; }
        public List<LookupClassModel> lookups { get; set; }
        public List<PickupListModel> pickuplist { get; set; }

        public bool IsLookup(string modelName)
        {
            foreach (var lookup in lookups)
            {
                if (lookup.Name.Equals(modelName)) return true;
            }
            return false;
        }

        public List<ClassModel> GetAllLookupClass(List<ClassModel> models)
        {
            List<ClassModel> list = new List<ClassModel>();
            foreach (ClassModel c in models)
            {
                ClassModel c2 = c.GetAllLookup();
                if (c2._attributes.Count > 0)
                {
                    list.Add(c2);
                }
            }
            return list;
        }

        public List<AttributeModel> GetAllLookupAttribute(ClassModel model)
        {
            List<AttributeModel> list = new List<AttributeModel>();
            foreach (AttributeModel c in model._attributes)
            {
                if (c.DataType.Name.Equals("Lookup Table"))
                {
                    list.Add(c);
                }
            }
            return list;
        }

        public List<AttributeModel> GetAllMasterDetailAttribute(ClassModel model)
        {
            List<AttributeModel> list = new List<AttributeModel>();
            foreach (AttributeModel c in model._attributes)
            {
                if (c.DataType.Name.Equals("Master Detail"))
                {
                    list.Add(c);
                }
            }
            return list;
        }

        /// <summary>
        /// Name ::: Mr. Akawit Tangkupatanonth
        /// Date ::: 16/03/2014
        /// Description ::: This function for check this class use validate in attribute at least one
        /// </summary>
        /// <returns>Boolean</returns>

        public bool CheckUseValidateInAttribute(ClassModel model)
        {
            List<AttributeModel> list = new List<AttributeModel>();
            foreach (AttributeModel c in model._attributes)
            {
                if (c.Validate || c.FieldType.Name == "URL Link")
                {
                    return true;
                }
            }
            return false;
        }

        /// <summary>
        /// Name : Mr. Akawit Tangkupatanonth
        /// Date : 19/03/2014
        /// Description : เช็คว่ามี Attribute ที่มีชนิดเป็น image กี่ตัว ถ้ามี ให้ใส่่ ,ชื่อAttribute ที่ parameter
        /// </summary>
        /// <param name="model"></param>
        /// <returns>String</returns>

        public String GetListValidateFieldTypeImage(bool checkHaveImages, List<AttributeModel> listImageAttrs)
        {
            String additionVariable = "";

            if (checkHaveImages)
            {
                foreach (AttributeModel listImageAttr in listImageAttrs)
                {
                    additionVariable += ", " + listImageAttr.Name + "s";
                }

                additionVariable += ", stateDelete";
            }

            return additionVariable;
        }

        /// <summary>
        /// Name : Phuwadet Boonpho
        /// Date : 19/03/2014
        /// Description : ตรวจสอบว่ามี Attribute ที่เป็น Image ใน Class หรือไม่
        /// </summary>
        /// <param name="model"></param>
        /// <returns>Boolean</returns>


        public bool CheckFieldTypeImage(ClassModel model)
        {
            List<AttributeModel> list = new List<AttributeModel>();
            foreach (AttributeModel c in model._attributes)
            {
                if (c.FieldType.Name == "Image")
                {
                    return true;
                }
            }
            return false;
        }


        /// <summary>
        /// Name : Phuwadet Boonpho
        /// Date : 19/03/2014
        /// Description : ตรวจสอบว่ามี Attribute ที่เป็น Image ทั้งหมดใน Class หรือไม่
        /// </summary>
        /// <param name="model"></param>
        /// <returns>Boolean</returns>

        public List<AttributeModel> ListFieldTypeImage(ClassModel model)
        {
            List<AttributeModel> list = new List<AttributeModel>();
            foreach (AttributeModel a in model._attributes)
            {
                if (a.FieldType.Name == "Image")
                {
                    list.Add(a);
                }
            }
            return list;
        }

        /// <summary>
        /// Name : Phuwadet Boonpho
        /// Date : 19/03/2014
        /// Description : เช็คว่ามี Attribute ที่มีชนิดเป็น image กี่ตัว ถ้ามี ให้ใส่่ ,HttpPostedFileBase[] ชื่อAttribute ที่ parameter
        /// </summary>
        /// <param name="model"></param>
        /// <returns>String</returns>

        public String GetListAddFieldTypeImage(bool checkHaveImages, List<AttributeModel> listImageAttrs)
        {
            String additionVariable = "";

            if (checkHaveImages)
            {
                foreach (AttributeModel listImageAttr in listImageAttrs)
                {
                    additionVariable += ", HttpPostedFileBase[] " + listImageAttr.Name + "s";
                }

                additionVariable += ", string stateDelete";
            }

            return additionVariable;
        }

		public bool HasGeoLocationAttribute()
		{
			foreach (AttributeModel attribute in Data.Models[0]._attributes)
			{
				if (attribute.DataType.Name.Equals("GeoLocation"))
				{
					return true;
				}
			}

			return false;
		} 


        public string Filename
        {
            get { return "{ClassName}Controller.cs"; }
        }

        public bool IsGenerateOneTime { get { return false; } }
        public bool IsGenerateFolder { get { return false; } }
        public bool IsRequiredOnlyOneMember { get { return true; } }
        public GenerateStyle GenerateStyle { get { return GenerateStyle.Normal; } }
    }
}

